﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Arizu.DAL;

namespace Arizu.Repository
{
    public class AirizuRoom : BaseProvider
    {

        #region
        private static volatile AirizuRoom instance;
        private static object lockHelper = new object();

        private AirizuRoom()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuRoom CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuRoom();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 获取行程
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetRoomInfos(long userId)
        {
            DataTable dt = new DataTable();
            string sql = "select Name, SendRoomid from SendRoom where OwnerID=" + userId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public DataTable GetSpecificRoom()
        {
            DataTable dt = new DataTable();
            string sql = "select top 2 RoomTitle, City, SendRoomid, RecommendedReason from SendRoom where IsTeSe=" + 1 + " ORDER BY NEWID()";

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public DataTable GetCity()
        {
            DataTable dt = new DataTable();
            string sql = "select distinct city from SendRoom";

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 获取行程
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetRoomInfos(string str)
        {
            DataTable dt = new DataTable();
            string sql = "select * from SendRoom where Province LIKE '%" + str + "%' or City LIKE'%" + str + "%'";

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public DataTable GetRoom(long roomId)
        {
            DataTable dt = new DataTable();
            string sql = "select * from SendRoom where SendRoomid=" + roomId;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        /// <summary>
        /// 获取行程
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DataTable GetRoomInfos(int isSend)
        {
            DataTable dt = new DataTable();
            string sql = "select * from SendRoom where Issend=" + isSend;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }

            return dt;
        }

        public void UpdateDayRent(long roomId, int rent)
        {

            string sql = "update PersonInfo set DayRent=" + rent + " where RoomId=" + roomId + "";

            Update(sql);

        }

        public void UpdateDayRent(long roomId, int rent, long start, int end)
        {
            string sql = "update PersonInfo set DayRent=" + rent + ", BargainBegin=" + start + ", BargainEnd=" + end + " where RoomId=" + roomId + "";

            Update(sql);
        }

        public void UpdateWeekRent(long roomId, int rent)
        {
            string sql = "update PersonInfo set WeekRent=" + rent + " where RoomId=" + roomId + "";

            Update(sql);
        }
        public void UpdateMonthRent(long roomId, int rent, int addtionRent)
        {
            string sql = "update PersonInfo set MonthRent=" + rent + ", AdditionalMonthlyRent=" + addtionRent + " where RoomId=" + roomId + "";

            Update(sql);
        }
        public void UpdateOverbookRent(long roomId, int rent)
        {
            string sql = "update PersonInfo set OverbookRent=" + rent + " where RoomId=" + roomId + "";

            Update(sql);
        }
        public void UpdateExtraRent(long roomId, int rent)
        {
            string sql = "update PersonInfo set ExtraRent=" + rent + " where RoomId=" + roomId + "";

            Update(sql);
        }

        public void UpdateExtraRent(long roomId, int rent, int num)
        {
            string sql = "update PersonInfo set ExtraRent='" + rent + "', OverbookNum=" + num + " where RoomId=" + roomId + "";

            Update(sql);
        }

        public void UpdateSendState(long roomId, int issend)
        {
            string sql = "update SendRoom set Issend=" + issend + " where SendRoomid=" + roomId + "";

            Update(sql);
        }

        public void Update(string sql) 
        {
            DataTable dt = new DataTable();

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        
    }
}
